﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Arborium;

namespace ArboriumExplorer
{
    public static class Conversions
    {
        public static void TryToDecode(StringBuilder convertedData, StringBuilder convertedMetadata, AElement node)
        {
            #region ASCII string

            TryConvert(d =>
            {
                string data = AData.AsAsciiString(node.Data);
                d.AppendFormat("As ASCII string: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                string metadata = AData.AsAsciiString(node.MetaData);
                m.AppendFormat("As ASCII string: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Bool

            TryConvert(d =>
            {
                bool data = AData.AsBool(node.Data);
                d.AppendFormat("Bool: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                bool metadata = AData.AsBool(node.MetaData);
                m.AppendFormat("Bool: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Byte

            TryConvert(d =>
            {
                byte data = AData.AsByte(node.Data);
                d.AppendFormat("Byte: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                byte metadata = AData.AsByte(node.MetaData);
                m.AppendFormat("Byte: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Char

            TryConvert(d =>
            {
                char data = AData.AsChar(node.Data);
                d.AppendFormat("Unicode character: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                char metadata = AData.AsChar(node.MetaData);
                m.AppendFormat("Unicode character: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region DateTime

            TryConvert(d =>
            {
                DateTime data = AData.AsDateTime(node.Data);
                d.AppendFormat("Date and time: {0}\n", data.ToString());
            }, convertedData);

            TryConvert(m =>
            {

                DateTime metadata = AData.AsDateTime(node.MetaData);
                m.AppendFormat("Date and time: {0}\n", metadata.ToString());
            }, convertedMetadata);

            #endregion

            #region Decimal

            TryConvert(d =>
            {
                decimal data = AData.AsDecimal(node.Data);
                d.AppendFormat("Decimal: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                decimal metadata = AData.AsDecimal(node.MetaData);
                m.AppendFormat("Decimal: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Double

            TryConvert(d =>
            {
                double data = AData.AsDouble(node.Data);
                d.AppendFormat("Double: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                double metadata = AData.AsDouble(node.MetaData);
                m.AppendFormat("Double: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Float

            TryConvert(d =>
            {
                float data = AData.AsFloat(node.Data);
                d.AppendFormat("Float: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                float metadata = AData.AsFloat(node.MetaData);
                m.AppendFormat("Float: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Guid

            TryConvert(d =>
            {
                Guid data = AData.AsGuid(node.Data);
                d.AppendFormat("GUID/UUID: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                Guid metadata = AData.AsGuid(node.MetaData);
                m.AppendFormat("GUID/UUID: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Int32

            TryConvert(d =>
            {
                int data = AData.AsInt(node.Data);
                d.AppendFormat("Integer: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {

                int metadata = AData.AsInt(node.MetaData);
                m.AppendFormat("Integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Int64

            TryConvert(d =>
            {
                long data = AData.AsLong(node.Data);
                d.AppendFormat("Long integer: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                long metadata = AData.AsLong(node.MetaData);
                m.AppendFormat("Long integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region Int16

            TryConvert(d =>
            {
                short data = AData.AsShort(node.Data);
                d.AppendFormat("Short integer: {0}\n", data);
            }, convertedData);
            TryConvert(m =>
            {
                short metadata = AData.AsShort(node.MetaData);
                m.AppendFormat("Short integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region UInt32

            TryConvert(d =>
            {
                uint data = AData.AsUInt(node.Data);
                d.AppendFormat("Unsigned integer: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                uint metadata = AData.AsUInt(node.MetaData);
                m.AppendFormat("Unsigned integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region UInt64

            TryConvert(d =>
            {
                ulong data = AData.AsULong(node.Data);
                d.AppendFormat("Unsigned long integer: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                ulong metadata = AData.AsULong(node.MetaData);
                m.AppendFormat("Unsigned long integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region UInt16

            TryConvert(d =>
            {
                ushort data = AData.AsUShort(node.Data);
                d.AppendFormat("Unsigned short integer: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                ushort metadata = AData.AsUShort(node.MetaData);
                m.AppendFormat("Unsigned short integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region UTF-8 string

            TryConvert(d =>
            {
                string data = AData.AsUtf8String(node.Data);
                d.AppendFormat("UTF-8 string: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                string metadata = AData.AsUtf8String(node.MetaData);
                m.AppendFormat("UTF-8 string: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region UTF-16LE string

            TryConvert(d =>
            {
                string data = AData.AsUtf16String(node.Data);
                d.AppendFormat("UTF-16LE string: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                string metadata = AData.AsUtf16String(node.MetaData);
                m.AppendFormat("UTF-16LE string: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region UTF-32LE

            TryConvert(d =>
            {
                string data = AData.AsUtf32String(node.Data);
                d.AppendFormat("UTF-32LE string: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                string metadata = AData.AsUtf32String(node.MetaData);
                m.AppendFormat("UTF-32LE string: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region VarInt

            TryConvert(d =>
            {
                long data = AData.AsVarInt(node.Data);
                d.AppendFormat("Variable length integer: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                long metadata = AData.AsVarInt(node.MetaData);
                m.AppendFormat("Variable length integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion

            #region VarUInt

            TryConvert(d =>
            {
                ulong data = AData.AsVarUInt(node.Data);
                d.AppendFormat("Variable length unsigned integer: {0}\n", data);
            }, convertedData);

            TryConvert(m =>
            {
                ulong metadata = AData.AsVarUInt(node.MetaData);
                m.AppendFormat("Variable length unsigned integer: {0}\n", metadata);
            }, convertedMetadata);

            #endregion
        }

        private static void TryConvert(Action<StringBuilder> action, StringBuilder data)
        {
            try
            {
                action(data);
            }
            catch (Exception)
            {
            }
        }
    }
}
